package cn.bdqn.hospital.dao.impl;

import cn.bdqn.hospital.dao.UserDao;
import cn.bdqn.hospital.entity.Comment;
import cn.bdqn.hospital.entity.User;
import cn.bdqn.hospital.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 通过id获取用户信息
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public User getUserByPwd(User user) throws SQLException {
        User user2 = null;
        try{
            String sql = "select * from user where hzName = ? and hzPwd = ?;";
            user2 = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<User>(User.class),user.getHzName(),user.getHzPwd());
        }catch (Exception e){
            e.printStackTrace();
        }
        return user2;
    }
    /**
     * 新增用户
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public int add(User user) throws SQLException {
        int num = 0;
        try {
            String sql = "INSERT INTO `user`(`hzName`, `hzPwd`, `hzSex`, `birth`, `hzPhone`, `shenFen`, `hzEamil`, `hzAddress`) " +
                    "VALUES (?,?,?,?,?,?,?,?);";
            num = queryRunner.update(DruidUtils.getConnection(),sql,user.getHzName(), user.getHzPwd(), user.getHzSex(),
                    user.getBirth(), user.getHzPhone(), user.getShenFen(), user.getHzEamil(), user.getHzAddress());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }
    /**
     * 修改用户信息
     */
    @Override
    public int update(User user) throws SQLException {
        int num = 0;
        String sql = "update user set hzName=?,hzPwd=?,hzSex=?,birth=?,hzPhone=?,shenFen=?,hzEamil=?,hzAddress=? where hzId = ?;";
        num = queryRunner.update(DruidUtils.getConnection(),sql,user.getHzName(), user.getHzPwd(),user.getHzSex(),user.getBirth(),user.getHzPhone(),user.getShenFen(),user.getHzEamil(),user.getHzAddress(),user.getHzId());
        return num;
    }
    /**
     * 根据用户id删除用户信息
     */
    @Override
    public int deleteByHzId(Integer hzId) throws SQLException {
        int num = 0;
        String sql = "delete from user where hzId =?;";
        num = queryRunner.update(DruidUtils.getConnection(), sql, hzId);
        return num;
    }
    /**
     * 根据预约id查询用户预约列表
     */
    @Override
    public List<User> getUsersByOrderId(Integer orderId, Integer pageNo, Integer pageSize) throws SQLException {
        List<User> userList = new ArrayList<User>();
        return userList;
    }

    /**
     * 根据名字查询用户列表
     */
    @Override
    public User getUserByHzName(String hzName) throws SQLException {
        return null;
    }
}
